var common = require("../../utils/Conmon.js");
var appData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    eyeopen: false,
    Account: '',
    Password: ''
    //returnurl: ''
  },
  eyeChange: function () {
    this.setData({
      eyeopen: !this.data.eyeopen
    });
  },

  /**
   * 获取登录账号
   */
  onAccountInput: function (e) {
    this.setData({
      Account: e.detail.value
    })
  },

  /**
   * 获取密码
   */
  onPasswordInput: function (e) {
    this.setData({
      Password: e.detail.value
    })
  },

  /**
   * 账号登录
   */
  onLogin: function () {
    if (this.data.Account == '' || this.data.Account.length < 4) {
      wx.showToast({
        title: '请准确填写账号',
        duration: 2000
      })
      return false;
    }
    if (this.data.Password == '' || this.data.Password.length < 6) {
      wx.showToast({
        title: '请准确填写密码',
        duration: 2000
      })
      return false;
    }
    var that = this;
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/member/api/login/webapplogin';
    wx.removeStorageSync('UserInfo');
    wx.request({
      url: url,
      data: { account: this.data.Account, password: this.data.Password, custid: custId },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 1200) {
          wx.setStorageSync('UserInfo', res.data.content);
          wx.switchTab({
            url: '/pages/myorders/myorders',
          })
        } else {
          wx.showToast({
            title: '账号或密码有误',
            duration: 2000
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '登录失败',
          duration: 2000
        })
      },
      complete: function () {
        //complete
      }
    })
  },

  /**
   * 微信登录
   */
  onWeChatLogin: function () {
    var unionId = wx.getStorageSync('unionId')
    if (!unionId) {
      wx.showModal({
        title: '微信登录',
        content: 'unionId不能为null',
        showCancel: false
      });
      return;
    }

    var that = this;
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    wx.removeStorageSync('UserInfo');
    wx.request({
      url: globalData.apiurl + '/member/api/register/WXLoginForSmallAPP',
      data: { custId: custId, unionId: unionId, memberSources:2},
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == 1200) {
          wx.setStorageSync('UserInfo', res.data.content);//存储userInfo  
          wx.switchTab({
            url: '/pages/customIndex/index',
          })
        } else {//注册失败
          wx.showToast({
            title: res.msg,
            duration: 2000
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '系统请求出错',
          duration: 2000
        })
      },
      complete: function () {
        //complete
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

//获取当前页url
function getCurrentPageUrl() {
  var pages = getCurrentPages()//获取加载的页面
  var currentPage = pages[pages.length - 1]//获取当前页面的对象
  var url = currentPage.route//当前页面url
  return url;
}